﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_BLL.Model;
using ANZ_AIM_GUI.Utils;
using System.IO;
using System.Reflection;
using ANZ_AIM_BLL;
using ANZ_AIM_FRAMEWORK;

namespace ANZ_AIM_GUI
{
    public partial class frmEmployeeOffline : Form
    {
        public EmploymentOffline employmentTemp { get; set; }
        public List<EmploymentOffline> listEmploymentOfflineTemp { get; set; }
        public DataTable dtEmploymentOfflineTemp { get; set; }

        public frmEmployeeOffline()
        {
            InitializeComponent();
        }

        public frmEmployeeOffline(DataTable dtEmploymentChange, List<EmploymentOffline> listEmploymentChange)
            : this()
        {
            try
            {
                dataGridViewX1.DataSource = null;
                dataGridViewX1.Columns.Clear();
                dataGridViewX1.DataSource = dtEmploymentChange;
                dataGridViewX1.AutoResizeColumns();

                dataGridViewX1.Columns[0].HeaderText = "Profile ID";
                dataGridViewX1.Columns[1].HeaderText = "Batch File";
                dataGridViewX1.Columns[2].HeaderText = "Field Change";
                dataGridViewX1.Columns[3].HeaderText = "Old Value";
                dataGridViewX1.Columns[4].HeaderText = "New Value";
                dataGridViewX1.Columns[5].Visible = false;


                this.listEmploymentOfflineTemp = listEmploymentChange;
                this.dtEmploymentOfflineTemp = dtEmploymentChange;

                this.employmentTemp = new EmploymentOffline();

            }
            catch (Exception ex)
            {
                throw;
            }
        }

        private void btnCloseChange_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnUpdateEmployment_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dataGridViewX1.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    if (string.IsNullOrEmpty(txtNewValue.Text))
                    {
                        MessageBox.Show("Nothing to change.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    //edit invidual
                    if (!string.IsNullOrEmpty(txtProfileIDChange.Text))
                    {
                        DataRow[] foundRows = dtEmploymentOfflineTemp.Select("ProfileID= '" + txtProfileIDChange.Text + "' AND FieldChange = '" + txtField.Text + "'");
                        if (foundRows.Length > 0)
                        {
                            foundRows[0]["NewValue"] = txtNewValue.Text;
                        }

                        int index = listEmploymentOfflineTemp.FindIndex(f => f.ProfileID.Equals(txtProfileIDChange.Text));
                        if (index >= 0)
                        {
                            employmentTemp = listEmploymentOfflineTemp[index];
                            employmentTemp.GetType().GetProperty(txtField.Text).SetValue(employmentTemp, txtNewValue.Text, null);
                        }
                    }

                    //edit batch file
                    if(!string.IsNullOrEmpty(txtBatchFile.Text))
                    {
                        DataRow[] foundRows = dtEmploymentOfflineTemp.Select("BatchFile= '" + txtBatchFile.Text + "' AND FieldChange = '" + txtField.Text + "'");
                        if (foundRows.Length > 0)
                        {
                            foundRows[0]["NewValue"] = txtNewValue.Text;
                        }

                        List<EmploymentOffline> listResult = listEmploymentOfflineTemp.FindAll(item => item.BatchFileID == txtBatchFile.Text);
                        if (listResult.Count >= 0)
                        {
                            foreach (EmploymentOffline eo in listResult)
                            {
                                eo.GetType().GetProperty(txtField.Text).SetValue(eo, txtNewValue.Text, null);
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Not employment is selected.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void dataGridViewX1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                int selectedRowCount = dataGridViewX1.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DataRow dr = ((DataRowView)dataGridViewX1.SelectedRows[0].DataBoundItem).Row;
                    txtProfileIDChange.Text = dr["ProfileID"].ToString();
                    txtBatchFile.Text = dr["BatchFile"].ToString();
                    txtField.Text = dr["FieldChange"].ToString();
                    txtOldValue.Text = dr["OldValue"].ToString();
                    txtNewValue.Text = dr["NewValue"].ToString();
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnDeleteChange_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dataGridViewX1.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DialogResult result = MessageBox.Show("Are you sure to delete this change?", "Delete confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                       MessageBoxDefaultButton.Button2);
                    if (result == DialogResult.Yes)
                    {
                        DataRow dr = ((DataRowView)dataGridViewX1.SelectedRows[0].DataBoundItem).Row;
                        string profileID = dr["ProfileID"].ToString();
                        string fieldChange = dr["FieldChange"].ToString();
                        string batchFile = dr["BatchFile"].ToString();

                        //delete invidual change
                        if (!string.IsNullOrEmpty(profileID))
                        {
                           
                            DataRow[] foundRows = dtEmploymentOfflineTemp.Select("ProfileID= '" + profileID + "' AND FieldChange = '" + fieldChange + "'");
                            string firstValue = "";
                            if (foundRows.Length > 0)
                            {
                                firstValue = foundRows[0]["FirstValue"].ToString();
                                dtEmploymentOfflineTemp.Rows.Remove(foundRows[0]);
                            }

                            int index = listEmploymentOfflineTemp.FindIndex(f => f.ProfileID.Equals(profileID));
                            if (index >= 0)
                            {
                                employmentTemp = listEmploymentOfflineTemp[index];
                                employmentTemp.GetType().GetProperty(fieldChange).SetValue(employmentTemp, firstValue, null);
                            }
                        }

                        //delete batch file change
                        if (!string.IsNullOrEmpty(batchFile))
                        {
                          
                            DataRow[] foundRows = dtEmploymentOfflineTemp.Select("BatchFile= '" + batchFile + "' AND FieldChange = '" + fieldChange + "'");
                            string firstValue = "";
                            if (foundRows.Length > 0)
                            {
                                firstValue = foundRows[0]["FirstValue"].ToString();
                                dtEmploymentOfflineTemp.Rows.Remove(foundRows[0]);
                            }

                            List<EmploymentOffline> listResult = listEmploymentOfflineTemp.FindAll(f => f.BatchFileID.Equals(batchFile));
                            if (listResult.Count > 0)
                            {
                                foreach (EmploymentOffline eo in listResult)
                                {
                                    eo.GetType().GetProperty(fieldChange).SetValue(eo, firstValue, null);
                                }
                            }
                        }
                    }                
                }
                else
                {
                    MessageBox.Show("Not employment is selected.", Constant.SYS_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
